<?php


namespace App\Repositories;


use App\Models\Product as Model;
use Exception;
use LTools\Repositories\RepositoryAbstract;

class Product extends RepositoryAbstract
{
    /**
     * Product constructor.
     * @param Model $model
     * @author luffyzhao@vip.126.com
     */
    public function __construct(Model $model)
    {
        $this->model = $model;
    }

    /**
     * @param array $attributes
     * @return Model|bool|\Illuminate\Database\Eloquent\Model
     * @author luffyzhao@vip.126.com
     */
    public function create(array $attributes = [])
    {
        /** @var Model $model */
        $model = parent::create($attributes);
        if (array_key_exists('images', $attributes)) {
            $model->images()->createMany(
                $attributes['images']
            );
        }
        $model->logs()->create(['remark' => '添加成功']);
        return $model;
    }

    /**
     * @param $id
     * @return \Illuminate\Database\Eloquent\Model
     * @author luffyzhao@vip.126.com
     */
    public function edit($id)
    {
        $model = $this->find($id);
        $model->loadMissing(['images']);
        return $model;
    }

    /**
     * @param $id
     * @param array $values
     * @return bool|\Illuminate\Database\Eloquent\Model
     * @throws \Throwable
     * @author luffyzhao@vip.126.com
     */
    public function update($id, array $values)
    {
        /** @var Model $model */
        $model = $this->lock($id);
        if ($model->isDraft()) {
            $model->fill($values)->saveOrFail();
            $model->images()->delete();
            if (array_key_exists('images', $values)) {
                $model->images()->createMany(
                    $values['images']
                );
            }

            $model->logs()->create(['remark' => '更新成功']);

            return $model;
        }
        throw new Exception("不是草稿状态不能修改！");
    }

    /**
     * @param $id
     * @return Model
     * @throws \Throwable
     * @author luffyzhao@vip.126.com
     */
    public function affirm($id)
    {
        /** @var Model $model */
        $model = $this->lock($id);
        if ($model->isDraft()) {
            $model->setAttribute('status', 1);
            $model->saveOrFail();

            $model->logs()->create(['remark' => '确认成功']);
            return $model;
        }
        throw new Exception("不是草稿状态不能确认！");
    }

    /**
     * @param $id
     * @return Model
     * @throws \Throwable
     * @author luffyzhao@vip.126.com
     */
    public function productLock($id)
    {
        /** @var Model $model */
        $model = $this->lock($id);
        if ($model->isUnlock()) {
            $model->setAttribute('lock_status', 1);
            $model->saveOrFail();

            $model->logs()->create(['remark' => '锁定成功']);
            return $model;
        }
        throw new Exception("不是无锁状态不能锁定！");
    }

    /**
     * @param $id
     * @return Model
     * @throws \Throwable
     * @author luffyzhao@vip.126.com
     */
    public function unlock($id)
    {
        /** @var Model $model */
        $model = $this->lock($id);
        if ($model->isLock()) {
            $model->setAttribute('lock_status', 0);
            $model->saveOrFail();

            $model->logs()->create(['remark' => '解锁成功']);
            return $model;
        }
        throw new Exception("不是锁定状态不能解锁！");
    }

    /**
     * @param $id
     * @param array $values
     * @return Model
     * @throws \Throwable
     * @author luffyzhao@vip.126.com
     */
    public function simpleUpdate($id, array $values)
    {
        /** @var Model $model */
        $model = $this->lock($id);
        if ($model->isLock()) {
            $model->fill($values)->saveOrFail();
            $model->logs()->create(['remark' => '修改成功']);

            return $model;
        }
        throw new Exception("锁定的产品才可以简单修改！");
    }
}
